﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ImageList imageList = new ImageList();

            // 加载需要的图像
            imageList.Images.Add(Image.FromFile("file.png"));
            imageList.Images.Add(Image.FromFile("txt.png"));

            // 设置图像大小
            imageList.ImageSize = new Size(16, 16);

            // 将 ImageList 赋值给 TreeView 的 ImageList 属性
            treeView1.ImageList = imageList;
            listView1.LargeImageList = imageList;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            string rootPath = "C:/Users/77247/Desktop/大二下/C#/WindowsFormsApp4/测试文件";
            DirectoryInfo rootDir = new DirectoryInfo(rootPath);

            TreeNode rootNode = new TreeNode(rootDir.Name);
            rootNode.ImageIndex = 0;
            rootNode.SelectedImageIndex = 0;
            rootNode.Tag = rootDir.FullName;
            treeView1.Nodes.Add(rootNode);

            this.AddTreeNodes(rootDir, rootNode);
        }

        private void AddTreeNodes(DirectoryInfo parentDir, TreeNode parentNode)
        {
            try
            {
                foreach (DirectoryInfo dir in parentDir.GetDirectories())
                {
                    TreeNode node = new TreeNode(dir.Name);
                    node.ImageIndex = 0;
                    node.SelectedImageIndex = 0;
                    node.Tag = dir.FullName;

                    parentNode.Nodes.Add(node);

                    this.AddTreeNodes(dir, node);
                }

                foreach (FileInfo file in parentDir.GetFiles("*.txt"))
                {
                    TreeNode node = new TreeNode(file.Name);
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                    node.Tag = file.FullName;

                    parentNode.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            string filePath = (string)listView1.SelectedItems[0].Tag;
            if (File.Exists(filePath))
            {
                try
                {
                    Process.Start(filePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();

            // 获取当前被选中节点的Tag属性
            string selectedPath = (string)e.Node.Tag;

            // 判断选中节点是否为文件夹节点
            if (!Directory.Exists(selectedPath))
            {
                return;
            }

            // 遍历该文件夹下的所有子文件夹和文件
            foreach (string dirPath in Directory.GetDirectories(selectedPath))
            {
                ListViewItem item = new ListViewItem(Path.GetFileName(dirPath));
                item.ImageIndex = 0; // 设置文件夹图标
                item.Tag = dirPath;
                listView1.Items.Add(item);
            }

            foreach (string filePath in Directory.GetFiles(selectedPath))
            {
                string fileExt = Path.GetExtension(filePath).ToLower();
                int imageIndex = 0; // 默认使用txt文件图标

                // 根据不同的文件类型设置对应的图标
                if (fileExt == ".txt")
                {
                    imageIndex = 1; // txt文件图标
                }

                ListViewItem item = new ListViewItem(Path.GetFileName(filePath));
                item.ImageIndex = imageIndex;
                item.Tag = filePath;
                listView1.Items.Add(item);
            }
        }
    }
}
